﻿var app = angular.module('myAPP');
app.controller('ActivateController', function ($scope, $routeParams, $http, $location) {

    $scope.init = function () {
        $scope.$parent.doselProfile();
        $scope.memberId = $routeParams.memberId;
        $http({
            url: "../../PaydSw.asmx/GetUserByMemberId",
            dataType: 'json',
            method: 'POST',
            data: {
                memberId: $scope.memberId

            },
            headers: {
                "Content-Type": "application/json"
            }
        }).success(function (response) {
            var data = JSON.parse(response.d);
            if (data.Result == 1) {
                if (data.objData.RegisterFlag == 'W') {
                    $scope.memberId = data.objData.MemberId;
                    $scope.idCard = data.objData.IdCard;
                    $scope.title = data.objData.Title;
                    $scope.name = data.objData.Name;
                    $scope.zipCode = data.objData.ZipCode;
                    $scope.address = data.objData.Addr1 + ' ' + data.objData.Addr2;
                    $scope.mobile = data.objData.MobileNo;
                    $scope.emailPrimary = data.objData.EmailPrimary;
                    $scope.emailSecondary = data.objData.EmailSecondary;
                }
                else if (data.objData.RegisterFlag == 'A') {
                    $scope.$parent.openAlertLogin(1, "ข้อความแจ้งเตือน", "ไม่สามารถ Activate ได้ เนื่องจาก Activate เสร็จเรียบร้อยแล้ว");
                }
                else if (data.objData.RegisterFlag == 'N') {
                    $scope.$parent.openAlertLogin(1, "ข้อความแจ้งเตือน", "ไม่สามารถ Activate ได้ เนื่องจากยังไม่ได้สมัครสมาชิก");
                }
            }


        })
        .error(function (error) {

            var ErrorMessage = 'error at init at ActivateController ' + error.ExceptionType + ' ' + error.Message + ' ' + error.StackTrace;
            alert(ErrorMessage);
            $scope.$parent.doselHome();
            $location.path('/home');
        });
    };
    $scope.activateAction = function () {
        $http({
            url: "../../PaydSw.asmx/ActivateUser",
            dataType: 'json',
            method: 'POST',
            data: {
                memberId: $scope.memberId

            },
            headers: {
                "Content-Type": "application/json"
            }
        }).success(function (response) {
            if (response.d == 1) {
                $scope.$parent.openAlertLogin(1, "Activate ได้สำเร็จ", "Activate เรียบร้อยแล้ว");
            }
            else {
                $scope.$parent.openAlertLogin(1, "ข้อความแจ้งเตือน", "ไม่สามารถ Activate, โปรดเช็ด");
            }

        })
        .error(function (error) {

            var ErrorMessage = 'error at activateAction at ActivateController ' + error.ExceptionType + ' ' + error.Message + ' ' + error.StackTrace;
            alert(ErrorMessage);
            $scope.$parent.doselHome();
            $location.path('/home');
        });
    };
});